/**
 * class name: pop,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * required-js: http://jsease.googlecode.com/svn/trunk/hc.js, mask.js,
 * update: http://jsease.googlecode.com/svn/trunk/slide.js
 *
        var ifm='<iframe style="position:absolute;width:2048px;height:1536px;top:50%;left:50%;margin:-768px 0 0 -1024px;border:none 0;z-index:99;" scrolling="no" frameborder="0"></iframe>';
        _.$.innerHTML=ifm;
 */
function pop(){/* {id,css,ico,title,x,data} */
    var A = arguments,
        _ = this;
    _ = ext( _, ext({
        id : 'pop'+ now(3),
        css : 'position:absolute;top:0;left:0;',
        ico : {src:'',css:''},
        width:300,
        height:300,
        close : '/icos/ishut.gif',
        title : '',
        nomask : false,
        maskcolor : '#000',
        noclose : false,
        cmdcss : 'width:75px;height:28px;margin:0 6px 0 0;',
        type : '',
        data : ''
        }, A[0] ) );
    _.uitype = 'pop';
    _.bound = {};
    _.z = now().getFullYear()+1;

    _.move = function(ev){
        var ev = evt(ev);
        me.all && _.tit.setCapture();
        _.pz.left = ev.X-_.pz._x<=0 || _.pz.W<=_.pz.width ? 0 : ev.X-_.pz._x+_.pz.width>=_.pz.W ? _.pz.W-_.pz.width : ev.X-_.pz._x;
        _.pz.top = ev.Y-_.pz._y<=0 || _.pz.H<=_.pz.height ? 0 : ev.Y-_.pz._y+_.pz.height>=_.pz.H ? _.pz.H-_.pz.height : ev.Y-_.pz._y;
        _.$.style.top = _.pz.top +'px';
        _.$.style.left = _.pz.left +'px';
    };

    _.stop = function(){
        try {
            unbind(me, 'mousemove', _.move);
            unbind(me, 'mouseup', _.stop);
            if ( $$.msie ) {
                me.all && _.tit.releaseCapture();
                me.onselectstart = function(){;};
            }
        } catch (e) {}
    };

    _.tomove = function(ev){
        if ( $$.msie ) {
            me.onselectstart = function(){
                me.selection.empty();
                event.returnValue = false;
            };
        }
        var ev = evt(ev);
        _.pz._y = ev.Y-_.pz.top;
        _.pz._x = ev.X-_.pz.left;
        bind(me, 'mousemove', _.move);
        bind(me, 'mouseup', _.stop);
    };

    _.repos = function(){
        _.$.style.cssText = _.css;
        _.pz = pos(_.$);
        _.$.style.top = _.pz.H<=_.pz.height ? 0 : (_.pz.H-_.pz.height)/2 +'px';
        _.$.style.left = _.pz.W<=_.pz.width ? 0 : (_.pz.W-_.pz.width)/2 +'px';
        _.pz = pos(_.$);
    return _;
    };

    _.setype = function(t){
        if ( !/normal|alert|confirm|form/i.test(t) ) {
            return _;
        }

        _.repos();
        _.body.style.height = _.pz.height-(/normal/i.test(t)?28:60) +'px';
        _.body.style.width = _.pz.width-2 +'px';

        if ( /normal/i.test(t) ) {
            if ( typeof _.bound.yes=='function' ) {
                unbind(_.yes,'click',_.bound.yes);
                delete _.bound.yes;
            }

            if ( typeof _.bound.no=='function' ) {
                unbind(_.no,'click',_.bound.no);
                delete _.bound.no;
            }

            if ( typeof _.bound.xx=='function' ) {
                unbind(_.xx,'click',_.bound.xx);
                delete _.bound.xx;
            }

            _.cmd && _.cmd.empty() && _.$.removeChild(_.cmd) && delete _.cmd;
            _.no && delete _.no;
            _.yes && delete _.yes;
            _.type = 'normal';
        return _;
        }

        if ( !_.cmd ) {
            _.cmd = elm('div').attr('align', 'right').appendTo(_.$)
                    .setClass('cmd');
        }

        if ( !hop(_, 'yes') || _.yes.nodeName!='BUTTON' ) {
            _.yes = elm('button').attr('type', 'button').appendTo(_.cmd);
        }

        _.yes.style.cssText = _.cmdcss;
        _.yes.innerHTML = '确定';
        _.yes.onclick = _.hide;
        _.type = 'alert';

        if ( /confirm|form/i.test(t) ) {
            if ( !hop(_,'no') || _.no.nodeName!='BUTTON' ) {
                _.no = elm('button').attr('type', 'button').appendTo(_.cmd);
            }
            _.no.style.cssText = _.cmdcss;
            _.no.innerHTML = '取消';
            _.no.onclick = _.hide;
            _.type = t;
        }

    return _;
    };

    _.setmsg = function(msg){
        _.body.empty();
        _.titxt.empty();
        switch ( _.type ) {
            case 'alert':
                _.icon.style.backgroundImage = 'url(/icos/information.png)';
                _.titxt.innerHTML = _.title || 'Alert ....';

                if ( !_.msg || _.msg.nodeName!='DIV' ) {
                    _.msg = elm('div').setClass('msg');
                }
                _.msg.appendTo(_.body);
                break;
            case 'confirm':
                _.icon.style.backgroundImage = 'url(/icos/exclamation.png)';
                _.titxt.innerHTML = _.title || 'Confirm ....';

                if ( !_.msg || _.msg.nodeName!='DIV' ) {
                    _.msg = elm('div').setClass('msg');
                }
                _.msg.appendTo(_.body);
                break;
            case 'form':
                if ( hop(_, 'msg') ) {
                    delete _.msg;
                }
                _.icon.style.backgroundImage = _.ico.src ? 'url('+ _.ico.src
                    +')' : 'url(/icos/application_form.png)';
                _.titxt.innerHTML = _.title || 'form';

                break;
            default:
                if ( hop(_, 'msg') ) {
                    delete _.msg;
                }
                _.icon.style.backgroundImage = 'url('+ _.ico.src +')';
                _.titxt.innerHTML = _.title;
                break;
        }

        var box = _.msg || _.body;
        if ( typeof msg =='function' ) {
            _.htm = msg.apply(box, [_.$]);
            if ( typeof _.htm=='string' ) {
                box.innerHTML = _.htm;
            }
        } else if ( typeof msg=='string' ) {
            box.innerHTML = msg;
        } else if ( msg.nodeType ) {
            box.appendChild(msg);
        }

    return _;
    };

    _.alert = function(msg){
        if ( _.type=='confirm' ) {
            if ( typeof _.bound.yes=='function' ) {
                unbind(_.yes, 'click', _.bound.yes);
                delete _.bound.yes;
            }

            if ( typeof _.bound.no=='function' ) {
                unbind(_.no, 'click', _.bound.no);
                delete _.bound.no;
            }
            _.cmd.removeChild(_.no);
            _.no = null;
            delete _.no;

            if ( typeof _.bound.xx=='function' ) {
                unbind(_.xx, 'click', _.bound.xx);
                delete _.bound.xx;
            }
        }
        _.setype('alert');
        _.setmsg(msg);
    return _.show();
    };

    _.confirm = function(msg, fy, fx){
        if ( _.type!='confirm' ) {
            _.setype('confirm');

            if ( typeof fy=='function' ) {
                bind(_.yes, 'click', fy);
                _.bound.yes = fy;
            }

            if ( typeof fx=='function' ) {
                bind(_.no, 'click', fx);
                bind(_.xx, 'click', fx);
                _.bound.xx = _.bound.no = fx;
            }
            _.setmsg(msg);
        }
    return _.show();
    };

    _.form = function (fns) {
        if ( _.type !='form' ) {
            _.setype('form');
        }

        try {
            _.yes.onclick = function (){
                var fo = geTags(_.body, 'form')[0];
                if ( fo.onsubmit()===false ) {
                    return false;
                } else {
                    if ( fo.action=='' ) {
                        return false;
                    }

                    var fhr = aja$( {url: fo.action, data: jsonForm(fo)}, {finish:
                            function (a){
                                var su = uesc(a);
                                if (  hop(fns, su) ) {
                                    fns[su]();
                                } else {
                                    _.hide();
                                }
                            }
                        });
                }
            };
        } catch (ex) {
        }

    return _.show();
    };

    _.show = function(msg){
        if ( typeof msg=='object' ) {
            if ( hop(msg, 'ico') ) {
                _.ico.src = msg.ico;
            }

            if ( hop(msg, 'title') ) {
                _.title = msg.title;
            }

            if ( hop(msg, 'css') ) {
                _.css = msg.css;
            }

            _.setype('normal');
            hop(msg, 'data') && _.setmsg(msg.data);
        }

        _.mask && _.mask.show();
        _.$.style.zIndex = _.z;
        _.$.style.display = '';
    return _;
    };

    _.hide = function(){
        _.mask && _.mask.hide();
        _.$.style.zIndex = -1;
        _.$.style.display = 'none';
    return _;
    };

    _.init = function(){
        _.$ = elm('div').before(me.body.childNodes[0]).setClass('popup');
        _.$.style.cssText = _.css;
        _.$.style.zIndex = -1;
        _.id = _.$.id = p(_, 'id') && !$(_.id) ? _.id : ('pop_'+ now(3));

        _.repos();

        if ( !_.nomask ) {
            _.mask = new mask({id:(_.id +'masker'), color:_.maskcolor||'#fff'}).init().hide();
        }

        if ( $$.msie && $$.version<7.0 ) {
            var ifm = elm('iframe');
            ifm.frameBorder = '0';
            ifm.scrolling = 'no';
            _.$.appendChild(ifm);
            with ( ifm.style ) {
                position = 'absolute';
                top = left = '0';
                width = _.pz.width +'px';
                height = _.pz.height +'px';
                marginTop = '0';
                marginLeft = '0';
                border = '0';
                zIndex = '-1';
                overflow = 'hidden';
            }

            _._ = elm('div').appendTo(_.$);
            _._.style.cssText = 'position:absolute;width:100%;height:100%;z-index:'+ _.z +';';
        }

        /* popup title */
        _.tit = elm('div').setClass('tit').appendTo(_.$);
        _.tit.style.zIndex = _.z;
        bind(_.tit, 'mousedown', _.tomove);

        _.icon = elm('span').setClass('ico').appendTo(_.tit);
        _.icon.style.cssText = 'background:url('+ _.ico.src +') 0 0 no-repeat;';

        _.titxt = elm('div').setClass('popup-title-text').appendTo(_.tit);

        _.icon.onmousedown = _.titxt.onmousedown = function() {
            me.selection && me.selection.empty();
            return false;
        };

        if ( !_.noclose ) {
            _.xx = elm('span').setClass('xx').appendTo(_.tit);
            _.xx.style.cssText = 'background:url('+ _.close +') 0 0 no-repeat;';

            _.xx.onmouseover = function(){
                this.style.backgroundPosition = '-16px 0';
            };

            _.xx.onmouseout = function(){
                this.style.backgroundPosition = '0 0';
            };

            _.xx.onclick = _.hide;

        }

        /* popup body */
        _.body = elm('div').setClass('content').appendTo(_.$);
        with ( _.body.style ) {
            width = _.pz.width-2 +'px';
            height = _.pz.height-28 +'px';
            zIndex = _.z;
            overflow = 'hidden';
        }

        p(_, 'type') && _.setype(_.type);

        _.setmsg(_.data);

    return _.hide();
    };

}